
interface ItreeSetting {
  key: string;
  parentKey: string;
  childKey: string;
}

//生成tree数据
export const transformTozTreeFormat = (sNodes: any, setting: ItreeSetting) => {
  const { key = 'id', parentKey = 'pid', childKey = 'child' } = setting;
  let i, l;
  if (!key || key == '' || !sNodes) return [];

  if (Array.isArray(sNodes) && sNodes.length) {
    const r = [];
    const tmpMap = {};
    for (i = 0, l = sNodes.length; i < l; i++) {
      tmpMap[sNodes[i][key]] = sNodes[i];
    }
    for (i = 0, l = sNodes.length; i < l; i++) {
      if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
        if (!tmpMap[sNodes[i][parentKey]][childKey])
          tmpMap[sNodes[i][parentKey]][childKey] = [];
        tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
      } else {
        // r.push(sNodes[i]);
      }
    }
    return r;
  } else {
    return [];
  }
};
